//
//  MyWelfareViewController.m
//  suRead
//
//  Created by Kobe LI on 16/4/23.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "MyWelfareViewController.h"
#import "WelfareDetailViewController.h"
#import "UIViewController+HUD.h"
#import "MJRefresh.h"

#import "MyWelfareViewTableViewCell.h"

#import "AccountManager.h"
#import "ServiceUser.h"
#import "M_Fuli.h"

#import "UIImageView+WebCache.h"

@interface MyWelfareViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *iconView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@property (weak, nonatomic) IBOutlet UITextField *codeText;

@property (weak, nonatomic) IBOutlet UITableView *infoTableView;

@property (nonatomic,strong) NSMutableArray *array;

@property (nonatomic,strong)NSURLSessionDataTask *requestHandle;
@property (nonatomic,assign)NSInteger currentPageIndex;
@end

@implementation MyWelfareViewController


- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)okButtonOnClick:(id)sender {
    
    if ([_codeText.text isEqualToString:@""]) {
        [self showHUDText:@"请输入福利码"];
        return;
    }
    
    [self showHUDProgress:@""];
    [ServiceUser doInputFuliCode:[AccountManager sharedAccountManager].f_uid
                          recode:_codeText.text
                         success:^(id responseObject, NSString *message) {
                             [self removeHUDProgress];
                             [self showHUDText:message];
                             
                             [self refreshData];
                         }
                         failure:^(NSString *message) {
                             [self removeHUDProgress];
                             [self showHUDText:message];
                         }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    __weak typeof(self) weak_self = self;

    self.array = [NSMutableArray new];
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[AccountManager sharedAccountManager].f_user_img] placeholderImage:[UIImage imageNamed:@"icon_personal"]];
    
    self.iconView.layer.cornerRadius  = 36.0;//设置图片圆角的尺度
    self.iconView.layer.masksToBounds = YES;//没这句话它圆不起来
    self.iconImageView.layer.cornerRadius  = 32.0;//设置图片圆角的尺度
    self.iconImageView.layer.masksToBounds = YES;//没这句话它圆不起来
    
    self.infoTableView.delegate = self;
    self.infoTableView.dataSource = self;
    self.infoTableView.sectionFooterHeight = 0;
    
    self.infoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weak_self refreshData];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weak_self.infoTableView.mj_header beginRefreshing];
    });
}

- (void)refreshData
{
    __weak typeof(self) weak_self = self;

    self.currentPageIndex = 0;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    [ServiceUser doGetMyFuLi:[AccountManager sharedAccountManager].f_uid
                   pageindex:_currentPageIndex
                    pagesize:10
                     success:^(id responseObject, NSString *message) {
                         [weak_self.infoTableView.mj_header endRefreshing];
                         NSArray *array = (NSArray *)responseObject;
                         weak_self.array = [NSMutableArray array];
                         if (weak_self.array) {
                             [weak_self.array addObjectsFromArray:array];
                             if (array.count == 10) {
                                 self.infoTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                                     [weak_self loadMore];
                                 }];
                             }
                         }
                         
                         [weak_self.infoTableView reloadData];
                     }
                     failure:^(NSString *message) {
                         [weak_self.infoTableView.mj_header endRefreshing];
                     }];
}

- (void)loadMore
{
    self.currentPageIndex ++;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    
    __weak typeof(self) weak_self = self;
    
    [ServiceUser doGetMyFuLi:[AccountManager sharedAccountManager].f_uid
                   pageindex:_currentPageIndex
                    pagesize:10
                     success:^(id responseObject, NSString *message) {
                         [weak_self.infoTableView.mj_header endRefreshing];
                         NSArray *array = (NSArray *)responseObject;
                         if (weak_self.array) {
                             [weak_self.array addObjectsFromArray:array];
                         }
                         
                         [weak_self.infoTableView reloadData];
                     }
                     failure:^(NSString *message) {
                         [weak_self.infoTableView.mj_footer endRefreshing];
                     }];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([UIScreen mainScreen].bounds.size.width - 60 - 30 ) * 0.36f;
;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_Fuli *fuli = [self.array objectAtIndex:indexPath.row];
    
    MyWelfareViewTableViewCell *cell = (MyWelfareViewTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyWelfareViewTableViewCell"];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:fuli.f_fuli_img] placeholderImage:nil];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    M_Fuli *fuli = [self.array objectAtIndex:indexPath.row];

    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    WelfareDetailViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"WelfareDetailViewController"];
    controller.data_ = fuli;
    [self.navigationController pushViewController:controller animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
